package breeze.lepei.der.utils;

import java.io.File;
import android.content.Context;
import java.io.IOException;

public class MLog {
	
	private static Context mContext;//上下文
	private static String filePath;//路径
	
	public MLog(Context context){
		this.mContext=context;
		filePath=AppTools.getAppLogFilePath(mContext);
		File file=new File(filePath);
		if(!file.exists()){
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
    
	/**
	*@author BREEZE
	*@description 写入数据
	*@param 新写入数据
	*/
	private static void writeLog(String newData){
		try {
			String oldData=AppTools.getDataFromFile(AppTools.getAppLogFilePath(mContext));
			StringBuilder sb=new StringBuilder();
			sb.append(oldData).append(newData).append("\n");
			AppTools.writeDataToFile(sb.toString(),AppTools.getAppLogFilePath(mContext));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	*@author BREEZE
	*@description 普通log
	*@param 内容
	*/
	public static void log(String content){
		//e.g:15:12：xxxxx
		StringBuilder sb=new StringBuilder();
		sb.append(">").append("[").append(AppTools.getTime("yyyy-MM-dd HH:mm:ss")).append("]").append("	")
		.append(content);
		writeLog(sb.toString());
	}
	
	
	/**
	 *@author BREEZE
	 *@description 普通log
	 *@param TAG，内容
	 */
	public static void log(String TAG,String content){
		//e.g:15:12：xxxxx
		StringBuilder sb=new StringBuilder();
		sb.append(">").append("[").append(AppTools.getTime("yyyy-MM-dd HH:mm:ss")).append("]").append("	").append(TAG).append(" 	").append("：")
			.append(content);
		writeLog(sb.toString());
	}
	
	/**
	 *@author BREEZE
	 *@description 普通log
	 *@param TAG，内容
	 */
	public static void log(String TAG,String Title,String content){
		//e.g:15:12：xxxxx
		StringBuilder sb=new StringBuilder();
		sb.append(">").append("[").append(AppTools.getTime("yyyy-MM-dd HH:mm:ss")).append("]").append("	").append(TAG).append(" 	").append("：")
			.append(Title).append(content);
		writeLog(sb.toString());
	}
	
	/**
	*@author BREEZE
	*@description 写出BUG
	*@param 错误
	*/
	public static void logE(String TAG,Throwable throwbale){
		if(throwbale!=null){
			String classname=throwbale.getClass().getSimpleName();
			if("".equals(classname)){
				classname="UnkonwView";
			}
			log(classname,"————运行错误————");
			log(TAG,throwbale.toString());
		}
	}
	
	/**
	*@author BREEZE
	*@description 显示对话框
	*@param Context,错误
	*/
	public static void showBug(Context context,Throwable throwable){
		if(throwable!=null){
			logE(context.getClass().getSimpleName(),throwable);
			MDialog.show(mContext,"运行错误",throwable.toString());
		}
	}
	
}
